from PyQt5.QtWidgets import (QLabel, QComboBox, QLineEdit, QPushButton, 
                             QRadioButton, QButtonGroup, QVBoxLayout, QHBoxLayout)
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QFileDialog  # 新增导入

class UIBuilder:
    @staticmethod
    def build_ui(parent, pre_configs, config):
        """构建主界面布局（支持无预设模式）
        
        Args:
            parent (QWidget): 父窗口对象
            pre_configs (list): 预设配置列表
            config (ConfigParser): 全局配置对象
        """
        main_layout = QVBoxLayout()
        
        # 班级名称输入（基础功能）
        class_layout = QHBoxLayout()
        class_layout.addWidget(QLabel('班级名称:'))
        parent.class_input = QLineEdit()
        parent.class_input.setText(config['DEFAULT'].get('class_name', ''))  # 从配置读取默认班级名
        class_layout.addWidget(parent.class_input)
        main_layout.addLayout(class_layout)

        # 模板选择（根据预设数量动态显示）
        parent.template_combo = QComboBox()
        pre_config_num = len(pre_configs)
        if pre_config_num > 0:
            template_layout = QHBoxLayout()
            template_layout.addWidget(QLabel('选择模板:'))
            for config_item in pre_configs:
                parent.template_combo.addItem(config_item["pre_config_name"])
            template_layout.addWidget(parent.template_combo)
            main_layout.addLayout(template_layout)
        else:
            # 无预设时显示提示
            no_template_label = QLabel('未检测到预设模板，将使用基础模式')
            no_template_label.setStyleSheet("color: red;")
            main_layout.addWidget(no_template_label)

            # 新增：目标文件选择布局
            target_file_layout = QHBoxLayout()
            target_file_layout.addWidget(QLabel('目标文件:'))
            parent.target_file_input = QLineEdit()  # 用于显示文件路径
            target_file_btn = QPushButton('选择文件')
            target_file_btn.clicked.connect(lambda: parent.select_target_file())  # 绑定文件选择事件
            target_file_layout.addWidget(parent.target_file_input)
            target_file_layout.addWidget(target_file_btn)
            main_layout.addLayout(target_file_layout)

            # 新增：替换目标输入布局
            replace_target_layout = QHBoxLayout()
            replace_target_layout.addWidget(QLabel('替换目标:'))
            parent.replace_target_input = QLineEdit()
            parent.replace_target_input.setText("[context]")  # 默认替换标记
            replace_target_layout.addWidget(parent.replace_target_input)
            main_layout.addLayout(replace_target_layout)

        # 模式选择（基础功能）
        mode_layout = QVBoxLayout()
        mode_layout.addWidget(QLabel('替换模式:'))
        parent.mode_group = QButtonGroup(parent)
        modes = [('班级+时间', '0'), ('仅班级', '1'), ('仅时间', '2')]
        for i, (text, value) in enumerate(modes):
            radio = QRadioButton(text)
            if value == config['DEFAULT'].get('mode', '0'):
                radio.setChecked(True)
            parent.mode_group.addButton(radio, i)
            mode_layout.addWidget(radio)
        main_layout.addLayout(mode_layout)

        # 打开方式（基础功能）
        open_layout = QVBoxLayout()
        open_layout.addWidget(QLabel('打开方式:'))
        parent.open_group = QButtonGroup(parent)
        open_methods = [('WPS Office', 'wps'), ('系统默认程序', 'default')]
        for i, (text, value) in enumerate(open_methods):
            radio = QRadioButton(text)
            if value == config['DEFAULT'].get('open_method', 'wps'):
                radio.setChecked(True)
            parent.open_group.addButton(radio, i)
            open_layout.addWidget(radio)
        main_layout.addLayout(open_layout)

        # 生成按钮（基础功能）
        button_layout = QHBoxLayout()
        parent.generate_btn = QPushButton('生成并打开')
        parent.generate_btn.clicked.connect(parent.generate_ppt)
        button_layout.addWidget(parent.generate_btn)
        main_layout.addLayout(button_layout)

        # 版权信息
        copyright_label = QLabel('© 2025 CreateCN <a href="https://gitee.com/CreateCN/pptx_builder">使用GPLv3开源</a>')
        copyright_label.setAlignment(Qt.AlignRight)
        copyright_label.setOpenExternalLinks(True)
        main_layout.addWidget(copyright_label)

        return main_layout